import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import Login from '@/views/login/loginPage.vue'
import Home from '@/views/layout/homePage.vue'
import Shop from '@/views/layout/shopPage.vue'
import Active from '@/views/layout/activePage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Home,
      redirect: '/shop',
      children: [
        {
          path: 'shop',
          component: Shop
        },
        {
          path: 'active',
          component: Active
        }
      ]
    }
  ],
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if ( !userStore.token && to.path !== '/login') {
    return '/login'
  }
})

export default router
